<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\MusicType;
use App\Models\MusicTypeMenu;
use Illuminate\Http\Request;

class MusicTypeMenuController extends Controller
{
    public function index(Request $request)
    {

        $musicTypes = MusicType::pluck("music_type_name", "id");

        $models = MusicTypeMenu::orderBy("music_type_id", "asc")
            ->orderBy("music_type_menu_id", "asc")
            ->orderBy("id", "asc")
            ->get();

        return view("admin.musicTypeMenu.index", [
            "models" => $models,
            "pageName" => "音乐分类菜单列表",
            "musicTypes" => $musicTypes,

        ]);
    }

    public function create(Request $request)
    {
        $musicTypes = MusicType::pluck("music_type_name", "id");



        return view("admin.musicTypeMenu.create", [
            "pageName" => "添加音乐分类菜单",
            "musicTypes" => $musicTypes,
        ]);
    }

    public function save(Request $request)
    {
        $model = new MusicTypeMenu();
        $model->music_type_id = $request->post("music_type_id");
        $model->music_type_menu_name = $request->post("music_type_menu_name");
        $model->music_type_menu_code = $request->post("music_type_menu_code");
        $model->music_type_menu_order = $request->post("music_type_menu_order");
//        $model->music_type_menu_id = $request->post("music_type_menu_id");
        $model->save();
        return redirect()->route("adminMusicTypeMenuIndex");

    }

    public function delete(Request $request)
    {
        $id = $request->input("id");
        MusicTypeMenu::destroy($id);
        return redirect()->route("adminMusicTypeMenuIndex");

    }

    public function edit(Request $request)
    {
        $id = $request->input("id");
        $model = MusicTypeMenu::find($id);
        $musicTypes = MusicType::pluck("music_type_name", "id");
        return view("admin.musicTypeMenu.edit", [
            "model" => $model,
            "pageName" => "编辑音乐分类菜单",
            "musicTypes" => $musicTypes,

        ]);
    }
    public function update(Request $request)
    {
        $id = $request->input("id");
        $model = MusicTypeMenu::find($id);
        $model->music_type_id = $request->post("music_type_id");
        $model->music_type_menu_name = $request->post("music_type_menu_name");
        $model->music_type_menu_code = $request->post("music_type_menu_code");
        $model->music_type_menu_order = $request->post("music_type_menu_order");
        $model->save();
        return redirect()->route("adminMusicTypeMenuIndex", ["musicId" => $model->music_id]);

    }
}
